package com.zhongxi.guoyuan.adapter.personCenter;

import android.content.Intent;
import android.os.Build;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.ItemOrderAdapter;
import com.zhongxi.guoyuan.entity.coinStore.AttrsSelect;
import com.zhongxi.guoyuan.entity.coinStore.PostcartAttrsCollection;
import com.zhongxi.guoyuan.entity.coinStore.SelcetPostCartGoodsAttrs;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.view.personalCenter.ExchangePayActivity;
import com.zhongxi.guoyuan.view.rewardCoin.ApplyRewardActivity;
import com.zhongxi.guoyuan.view.rewardCoin.CustomerNotesActivity;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/3/8.
 */

public class ExchangePayAdapter extends BaseAdapter {

    public static final int REQUESTNOTESCODE = 0302;
    private int payType;
    private AttrsSelect attrs;
    private ArrayList<PostcartAttrsCollection> goodsList;
    private ExchangePayActivity context;

    public ExchangePayAdapter(ExchangePayActivity context, ArrayList<PostcartAttrsCollection> goodsList, AttrsSelect attrs, int payType) {
        this.context = context;
        this.goodsList = goodsList;
        this.attrs = attrs;
        this.payType = payType;
    }

    @Override
    public int getCount() {
        if (goodsList != null && goodsList.size() > 0) {
            return goodsList.size();
        } else if (attrs != null && !"".equals(attrs)) {
            return 1;
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder vh;
        if (view == null) {
            view = View.inflate(context, R.layout.exchange_item_post_order, null);
            vh = new ViewHolder();
            vh.rl_top = (RelativeLayout) view.findViewById(R.id.rl_top);
            vh.tv_type = (TextView) view.findViewById(R.id.tv_type);
            vh.postOrder_name = (TextView) view.findViewById(R.id.postOrder_name);
            vh.tv_count = (TextView) view.findViewById(R.id.tv_count);
            vh.postOrder_lvList = (NoScrollListView) view.findViewById(R.id.postOrder_lvList);
            vh.ll_bg = (LinearLayout) view.findViewById(R.id.ll_bg);

            vh.ll_self_info = (LinearLayout) view.findViewById(R.id.ll_self_info);
            vh.tv_store_name = (TextView) view.findViewById(R.id.tv_store_name);
            vh.tv_store_phone = (TextView) view.findViewById(R.id.tv_store_phone);
            vh.tv_store_address = (TextView) view.findViewById(R.id.tv_store_address);

            vh.rl_scale = (RelativeLayout) view.findViewById(R.id.rl_scale);
            vh.tv_amountofmail = (TextView) view.findViewById(R.id.tv_amountofmail);
            vh.ll_notes = (LinearLayout) view.findViewById(R.id.ll_notes);
            vh.tv_notes = (TextView) view.findViewById(R.id.tv_notes);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        if (payType == 1) {
            vh.tv_type.setVisibility(View.VISIBLE);
            vh.tv_type.setText("包邮");

            vh.ll_self_info.setVisibility(View.GONE);
            vh.rl_scale.setVisibility(View.VISIBLE);
        } else if (payType == 2) {
            vh.tv_type.setText(" 同城 ");
            vh.rl_top.setVisibility(View.GONE);
            vh.ll_self_info.setVisibility(View.GONE);
            vh.rl_scale.setVisibility(View.INVISIBLE);
        } else {
            vh.tv_type.setVisibility(View.GONE);
            vh.rl_top.setVisibility(View.GONE);
            vh.ll_self_info.setVisibility(View.VISIBLE);
            vh.rl_scale.setVisibility(View.INVISIBLE);
        }

        vh.ll_notes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(context, CustomerNotesActivity.class);
                if (goodsList != null && goodsList.size() > 0) {
                    mIntent.putExtra("shopid", goodsList.get(position).getShopInfos().getId());
                } else {
                    mIntent.putExtra("shopid", attrs.getShopId());
                }
                context.startActivityForResult(mIntent, REQUESTNOTESCODE);
            }
        });
        if (goodsList != null && goodsList.size() > 0) {
            PostcartAttrsCollection postcartAttrsCollection = goodsList.get(position);
            if (postcartAttrsCollection.getShopInfos().getName() != null && !"".equals(postcartAttrsCollection.getShopInfos().getName())) {
                vh.postOrder_name.setText(postcartAttrsCollection.getShopInfos().getName());
            } else {
                vh.postOrder_name.setText("");
            }
            if (!TextUtils.isEmpty(postcartAttrsCollection.getCustomNotes())) {
                vh.tv_notes.setText(postcartAttrsCollection.getCustomNotes());
            }
            if (postcartAttrsCollection.getShopInfos().getStoreNum() != null && !"".equals(postcartAttrsCollection.getStoreNum())) {
                vh.tv_count.setText(postcartAttrsCollection.getShopInfos().getStoreNum() + " 件");
            } else {
                vh.tv_count.setText("0 件");
            }
            if (postcartAttrsCollection.getShopInfos().getName() != null && !"".equals(postcartAttrsCollection.getShopInfos().getName())) {
                vh.tv_store_name.setText(postcartAttrsCollection.getShopInfos().getName());
            } else {
                vh.tv_store_name.setText("");
            }
            if (postcartAttrsCollection.getShopInfos().getPhone() != null && !"".equals(postcartAttrsCollection.getShopInfos().getPhone())) {
                vh.tv_store_phone.setText(postcartAttrsCollection.getShopInfos().getPhone());
            } else {
                vh.tv_store_phone.setText("");
            }
            if (postcartAttrsCollection.getShopInfos().getAddress() != null && !"".equals(postcartAttrsCollection.getShopInfos().getAddress())) {
                vh.tv_store_address.setText(postcartAttrsCollection.getShopInfos().getAddress());
            } else {
                vh.tv_store_address.setText("");
            }

            if (TextUtils.isEmpty(postcartAttrsCollection.getCustomNotes())) {
                vh.tv_notes.setText("");
            } else {
                vh.tv_notes.setText(postcartAttrsCollection.getCustomNotes());
            }

            if (postcartAttrsCollection.getShopInfos().getAmountofmail() != null &&
                    DataUtil.moreThanZero(postcartAttrsCollection.getShopInfos().getAmountofmail())) {

                vh.rl_scale.setVisibility(View.VISIBLE);
                vh.tv_type.setVisibility(View.GONE);

            } else if (postcartAttrsCollection.getPostage() != null && !DataUtil.moreThanZero(postcartAttrsCollection.getPostage())) {
//                vh.tv_amountofmail.setText("本店商品包邮");
                vh.rl_scale.setVisibility(View.INVISIBLE);
                vh.tv_type.setVisibility(View.VISIBLE);
            } else {
                vh.rl_scale.setVisibility(View.INVISIBLE);
                vh.tv_type.setVisibility(View.GONE);
            }
            if (postcartAttrsCollection.getGoodsAttrses() != null && postcartAttrsCollection.getGoodsAttrses().size() > 0) {
                ItemOrderAdapter itemOrderAdapter = new ItemOrderAdapter(context,
                        postcartAttrsCollection.getGoodsAttrses(), attrs, payType);
                vh.postOrder_lvList.setAdapter(itemOrderAdapter);
            }
        } else if (attrs != null && !"".equals(attrs)) {
            List<SelcetPostCartGoodsAttrs> goodsAttrses = new ArrayList<>();
            if (attrs.getShopName() != null && !"".equals(attrs.getShopName())) {
                vh.postOrder_name.setText(attrs.getShopName());
            } else {
                vh.postOrder_name.setText("");
            }
            if (attrs.getGoodsnum() != null && !"".equals(attrs.getGoodsnum())) {
                vh.tv_count.setText(attrs.getGoodsnum() + " 件");
            } else {
                vh.tv_count.setText("0 件");
            }
            if (attrs.getShopName() != null && !"".equals(attrs.getShopName())) {
                vh.tv_store_name.setText(attrs.getShopName());
            } else {
                vh.tv_store_name.setText("");
            }
            if (attrs.getPhone() != null && !"".equals(attrs.getPhone())) {
                vh.tv_store_phone.setText(attrs.getPhone());
            } else {
                vh.tv_store_phone.setText("");
            }
            if (attrs.getAddress() != null && !"".equals(attrs.getAddress())) {
                vh.tv_store_address.setText(attrs.getAddress());
            } else {
                vh.tv_store_address.setText("");
            }

            if (TextUtils.isEmpty(attrs.getCustomNotes())) {
                vh.tv_notes.setText("");
            } else {
                vh.tv_notes.setText(attrs.getCustomNotes());
            }
            if (1 == payType) {
                if (null != attrs.getAmountofmail() && !"".equals(attrs.getAmountofmail()) && !"0".equals(attrs.getAmountofmail())) {

                    vh.rl_scale.setVisibility(View.VISIBLE);
                    vh.tv_type.setVisibility(View.GONE);

                    if (DataUtil.moreThanOther(DataUtil.multiplys(attrs.getPrice(), attrs.getGoodsnum(), 2), attrs.getAmountofmail())) {
                        vh.tv_amountofmail.setText("快递: 0 红豆");
                    } else {
                        vh.tv_amountofmail.setText("快递: " + attrs.getPostage() + " 元");
                    }
                } else if (attrs.getPostage() != null && !DataUtil.moreThanZero(attrs.getPostage())) {
                    vh.rl_scale.setVisibility(View.INVISIBLE);
                    vh.tv_type.setVisibility(View.VISIBLE);
                }else {
                    vh.rl_scale.setVisibility(View.VISIBLE);
                    vh.tv_type.setVisibility(View.GONE);
                    vh.tv_amountofmail.setText("快递: " + attrs.getPostage() + " 红豆");
                }
            }

            ExchangeItemOrderAdapter itemOrderAdapter =
                    new ExchangeItemOrderAdapter(context, goodsAttrses, attrs, payType);
            vh.postOrder_lvList.setAdapter(itemOrderAdapter);
        }
        return view;
    }


    class ViewHolder {
        private RelativeLayout rl_top;
        private TextView tv_type;
        private TextView postOrder_name;
        private TextView tv_count;
        private NoScrollListView postOrder_lvList;
        private LinearLayout ll_bg;

        private LinearLayout ll_self_info;
        private TextView tv_store_name;
        private TextView tv_store_phone;
        private TextView tv_store_address;

        private RelativeLayout rl_scale;
        private TextView tv_amountofmail;
        private LinearLayout ll_notes;
        private TextView tv_notes;

    }
}
